// components/classic/music/index.js
import {
	classicBeh
} from '../classic-beh'
// 获取音频管理器
const mMgr = wx.getBackgroundAudioManager()
Component({
	/**
	 * 组件的属性列表
	 */
	behaviors: [classicBeh],
	properties: {
		src: String,
		title:String
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		pauseSrc: 'images/player@pause.png',
		playSrc: 'images/player@play.png',
		playing: false
	},
	attached(){
		// 恢复音乐播放状态
		this._recoverStatus()
		this._monitorSwitch()
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		onPlay() {
			if (!this.data.playing) {
				this.setData({
					playing: true
				})
				mMgr.title = this.properties.title
				mMgr.src = this.properties.src;
			} else {
					this.setData({
						playing: false
					})
				mMgr.pause()
			}
		},
		// 恢复音乐播放状态封装
		_recoverStatus(){
			console.log(this.data.playing);
			// mMgr.paused 当前是否有暂停
			if(mMgr.paused){
				this.setData({
					playing:false
				})
				return
			}
			if(mMgr.src == this.properties.src){
				this.setData({
					playing:true
				})
				
			}
			
			
		},
		// 监听播放状态进行恢复封装
		_monitorSwitch(){
			mMgr.onPlay(()=>{
				console.log('play');
				this._recoverStatus();
			})
			mMgr.onPause(()=>{
				this._recoverStatus();
			})
			mMgr.onStop(()=>{
				console.log('onstop');
				this._recoverStatus();
			})
			mMgr.onEnded(()=>{
				console.log('onEnded');
				this._recoverStatus();
			})
		}
	}
})